<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateLayoutAPIRequest;
use App\Http\Requests\API\UpdateLayoutAPIRequest;
use App\Models\Category;
use App\Models\Goods;
use App\Models\Layout;
use App\Repositories\LayoutRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class LayoutController
 * @package App\Http\Controllers\API
 */

class LayoutAPIController extends ApiBaseController
{
    /** @var  LayoutRepository */
    private $layoutRepository;
    protected $model_name = 'App\Models\Layout';
    protected $model_load_array = [
    ];

    public function __construct(LayoutRepository $layoutRepo)
    {
        $this->layoutRepository = $layoutRepo;
    }

    public function _after_get($array)
    {
        $array = $array->toArray();
        $data = [];
        foreach ($array as $v) {
            $one = null;
            $one = [];
            $one['image_type'] = $v['image_type'];
            $one['skip_type'] = $v['skip_type'];
            $one['image'] = $v['image'];
            $one['image2'] = $v['image2'];
            $one['image3'] = $v['image3'];
            $one['skip_id'] = $v['skip_id'];
            $one['skip2_id'] = $v['skip2_id'];
            $one['skip3_id'] = $v['skip3_id'];
            if($v['skip_type']==1){
                $_cate = Category::find($v['skip_id']);
                $one['category'] = $_cate?$_cate->toArray():null;
                if(isset($v['skip2_id'])){
                    $_cate = Category::find($v['skip2_id']);
                    $one['category2'] = $_cate?$_cate->toArray():null;
                }
                if(isset($v['skip3_id'])){
                    $_cate = Category::find($v['skip3_id']);
                    $one['category3'] = $_cate?$_cate->toArray():null;
                }
            }elseif ($v['skip_type']==2){
                $goods = null;
                $goods = Goods::find($v['skip_id']);
                if($goods){
                    $goods->attributesByStatus;
                    $_arr = $goods->toArray();
                    if(isset($_arr['attributes_by_status'])){
                        $_arr['attributes'] = $_arr['attributes_by_status'];
                        unset($_arr['attributes_by_status']);
                    }
                    $one['goods'] = $_arr;
                }else{
                    $one['goods'] = null;
                }
                if(isset($v['skip2_id'])){
                    $goods2 = null;
                    $goods2 =Goods::find($v['skip2_id']);
                    if($goods2){
                        $goods2->attributesByStatus;
                        $_arr = $goods2->toArray();
                        if(isset($_arr['attributes_by_status'])){
                            $_arr['attributes'] = $_arr['attributes_by_status'];
                            unset($_arr['attributes_by_status']);
                        }
                        $one['goods2'] = $_arr;
                    }else{
                        $one['goods2'] = null;
                    }
                }
                if(isset($v['skip3_id'])){
                    $goods3 = null;
                    $goods3 =Goods::find($v['skip3_id']);
                    if($goods3){
                        $goods3->attributesByStatus;
                        $_arr = $goods3->toArray();
                        if(isset($_arr['attributes_by_status'])){
                            $_arr['attributes'] = $_arr['attributes_by_status'];
                            unset($_arr['attributes_by_status']);
                        }
                        $one['goods3'] = $_arr;
                    }else{
                        $one['goods3'] = null;
                    }
                }
            }
            $data[] = $one;
        }
        return $data;
    }
}
